package com.stgy.lease.web.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.stgy.lease.common.exception.StgyException;
import com.stgy.lease.common.result.ResultCodeEnum;
import com.stgy.lease.common.utils.JwtUtil;
import com.stgy.lease.model.domain.*;
import com.stgy.lease.web.api.mapper.*;
import com.stgy.lease.web.api.service.*;
import com.stgy.lease.web.api.vo.AgreementDetailVo;
import com.stgy.lease.web.api.vo.AgreementItemVo;
import jakarta.servlet.http.HttpServletRequest;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.rmi.dgc.Lease;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author lingx
 * @description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
 * @createDate 2024-08-28 11:00:29
 */
@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {

    @Autowired
    private ViewAppointmentService viewAppointmentService;

    @Autowired
    private ApartmentInfoService apartmentInfoService;

    @Autowired
    private RoomInfoService roomInfoService;

    @Autowired
    private GraphInfoService graphInfoService;

    @Autowired
    private PaymentTypeService paymentTypeService;

    @Autowired
    private LeaseTermMapper leaseTermMapper;


    //获取个人租约基本信息列表
    @Override
    public List<AgreementItemVo> listItem(String token, HttpServletRequest request) {
        Map<String, Object> map = JwtUtil.parseToken(token, request);
        Long userId = Long.parseLong(map.get("userId").toString());

        ViewAppointment viewAppointment = viewAppointmentService.getOne(Wrappers.lambdaQuery(ViewAppointment.class)
                .eq(ViewAppointment::getUserId, userId)
                .last("limit 1"));

        String phone = viewAppointment.getPhone();

        List<AgreementItemVo> agreementItemVoList = baseMapper.selectAgreementItemByPhone(phone);

        agreementItemVoList.stream().forEach(agreementItemVo -> {
            if (agreementItemVo.getLeaseStatus() == 5 ||
                    agreementItemVo.getLeaseStatus() == 2 ||
                    agreementItemVo.getLeaseStatus() == 4) {
                agreementItemVo.setIsRenewable(true);
            } else {
                agreementItemVo.setIsRenewable(false);
            }
        });

        return agreementItemVoList;
    }

    //根据id获取租约详细信息
    @Override
    public AgreementDetailVo getDetailById(Long id) {
        AgreementDetailVo agreementDetailVo = new AgreementDetailVo();

        LeaseAgreement leaseAgreement = this.getById(id);
        BeanUtils.copyProperties(leaseAgreement, agreementDetailVo);

        Long apartmentId = leaseAgreement.getApartmentId();
        ApartmentInfo apartmentInfo = apartmentInfoService.getById(apartmentId);
        agreementDetailVo.setApartmentName(apartmentInfo.getName());

        List<GraphInfo> apartmentGraphInfos = graphInfoService.list(Wrappers.lambdaQuery(GraphInfo.class)
                .eq(GraphInfo::getItemId, apartmentId)
                .eq(GraphInfo::getItemType, 1));
        agreementDetailVo.setApartmentGraphVoList(apartmentGraphInfos);

        Long roomId = leaseAgreement.getRoomId();
        RoomInfo roomInfo = roomInfoService.getById(roomId);
        agreementDetailVo.setRoomNumber(roomInfo.getRoomNumber());

        List<GraphInfo> roomGraphInfos = graphInfoService.list(Wrappers.lambdaQuery(GraphInfo.class)
                .eq(GraphInfo::getItemId, roomId)
                .eq(GraphInfo::getItemType, 2));
        agreementDetailVo.setRoomGraphVoList(roomGraphInfos);

        PaymentType paymentType = paymentTypeService.getById(leaseAgreement.getPaymentTypeId());
        agreementDetailVo.setPaymentTypeName(paymentType.getName());
        agreementDetailVo.setLeaseTermMonthCount(paymentType.getPayMonthCount());

        LeaseTerm leaseTerm = leaseTermMapper.selectById(leaseAgreement.getLeaseTermId());
        agreementDetailVo.setLeaseTermUnit(leaseTerm.getUnit());

        return agreementDetailVo;
    }
}